import 'dart:async';

import 'package:flutter/material.dart';
import 'package:junyeyishu/ui/home/widgets/QGLimitWidget.dart';

import 'package:junyeyishu/utils/utils.dart';
import 'package:junyeyishu/widgets/QGWidget.dart';

class QGHomeCell extends StatefulWidget {
  Map? data;
  QGHomeCell({super.key, this.data});

  @override
  State<QGHomeCell> createState() => _QGHomeCellState();
}

class _QGHomeCellState extends State<QGHomeCell> {
  String? countdown;
  Timer? timer;
  String? ing;
  @override
  void dispose() {
    timer?.cancel();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
  }

  updateCountdown() {
    countdown = getCountdown(widget.data?["publish_time"]);
    if (countdown != null) {
      timer?.cancel();
      timer = Timer.periodic(const Duration(seconds: 1), (timer) {
        countdown = getCountdown(widget.data?["publish_time"]);
        if (countdown == null) {
          timer.cancel();
        }
        if (mounted) {
          setState(() {});
        }
      });
    } else {}
  }

  @override
  Widget build(BuildContext context) {
    if (inReview()) {
      widget.data?["price"] = "";
    }

    updateCountdown();
    return Container(
      margin: EdgeInsets.fromLTRB(15.rpx, 0, 15.rpx, 20.rpx),
      decoration: BoxDecoration(borderRadius: BorderRadius.circular(10.rpx)),
      child: Stack(
        children: [
          Positioned(
            bottom: 0,
            child: QGImage(asset: "home_cell_bg", width: 345.rpx),
          ),
          Container(
            padding: EdgeInsets.only(bottom: 10.rpx),
            child: Column(
              children: [
                Stack(
                  children: [
                    ClipRRect(
                      borderRadius: BorderRadius.circular(10.rpx),
                      child: AspectRatio(
                        aspectRatio: 345 / 244,
                        child: Container(color: const Color(0xFFFFFFFF)),
                      ),
                    ),
                    ClipRRect(
                      borderRadius: BorderRadius.circular(10.rpx),
                      child: AspectRatio(
                        aspectRatio: 345 / 244,
                        child: QGImage(url: widget.data?["thumb_img"]),
                      ),
                    ),
                  ],
                ),
                SizedBox(height: 18.rpx),
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    SizedBox(width: 20.rpx),
                    Flexible(
                      child: Text(
                        widget.data?["name"] ?? "",
                        style: TextStyle(
                            color: const Color(0xFF111111),
                            fontSize: 16.rpx,
                            fontWeight: FontWeight.bold),
                      ),
                    ),
                    SizedBox(width: 10.rpx),
                    QGLimitWidget(num: widget.data?["total"] ?? ""),
                    SizedBox(width: 10.rpx),
                  ],
                ),
                SizedBox(height: 18.rpx),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Expanded(
                      child: Row(
                        children: [
                          SizedBox(width: 20.rpx),
                          ClipRRect(
                            borderRadius: BorderRadius.circular(12.rpx),
                            child: QGImage(
                                url: widget.data?["artist_avatar"],
                                width: 24.rpx,
                                height: 24.rpx),
                          ),
                          SizedBox(width: 6.rpx),
                          Expanded(
                            child: Text(
                              widget.data?["artist_name"] ?? "",
                              style: TextStyle(
                                  color: const Color.fromRGBO(17, 17, 17, 0.6),
                                  fontSize: 12.rpx,
                                  fontWeight: FontWeight.normal),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(right: 26.rpx),
                      child: Text(
                        '￥${widget.data?["price"]}',
                        style: TextStyle(
                            color: const Color(0xFF111111),
                            fontSize: 22.rpx,
                            fontWeight: FontWeight.bold),
                      ),
                    ),
                  ],
                )
              ],
            ),
          ),
          buildSoldout(),
          buildCountdown(),
        ],
      ),
    );
  }

  buildState(state) {
    return Positioned(
        top: 16.rpx,
        left: 16.rpx,
        child: ClipRRect(
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: 12.rpx),
            height: 34.rpx,
            decoration: BoxDecoration(
                gradient: const LinearGradient(
                  begin: Alignment.centerLeft,
                  end: Alignment.centerRight,
                  colors: [Color(0xFFFFF25A), Color(0xFFDCFF72)],
                ),
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(10.rpx),
                    topRight: Radius.circular(4.rpx),
                    bottomRight: Radius.circular(10.rpx),
                    bottomLeft: Radius.circular(4.rpx))),
            child: Row(children: [
              Text(
                state ?? "",
                style: TextStyle(
                    color: const Color(0xFF111111),
                    fontSize: 12.rpx,
                    fontWeight: FontWeight.normal),
              ),
            ]),
          ),
        ));
  }

  buildCountdown() {
    if (inReview()) {
      return Container();
    }

    if (countdown == null) {
      return Container();
    }
    return buildState("即将开售：$countdown");
  }

  buildSoldout() {
    if (inReview()) {
      return Container();
    }

    if (widget.data?["isSoldOut"] != 1) {
      if (countdown == null) {
        return buildState("进行中");
      }

      return Container();
    }

    return Positioned(
        top: 16.rpx,
        left: 16.rpx,
        child: ClipRRect(
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(10.rpx),
              topRight: Radius.circular(4.rpx),
              bottomRight: Radius.circular(10.rpx),
              bottomLeft: Radius.circular(4.rpx)),
          child: Container(
            width: 80.rpx,
            height: 34.rpx,
            color: const Color.fromRGBO(0, 0, 0, 0.5),
            child: Row(children: [
              SizedBox(width: 12.rpx),
              QGImage(asset: "home_soldout"),
              SizedBox(width: 5.rpx),
              Text(
                '已售罄',
                style: TextStyle(
                    color: Colors.white,
                    fontSize: 12.rpx,
                    fontWeight: FontWeight.normal),
              ),
            ]),
          ),
        ));
  }
}
